package com.bj58.mis.apm.apm.scf.controller;

import com.bj58.data.roc.service.cube.CubeStep4Service;
import com.bj58.data.roc.service.project.CubeBaseService;
import com.bj58.data.roc.service.project.ProjectService;
import com.bj58.data.roc.service.project.model.CubeBase;
import com.bj58.data.roc.service.project.model.Project;
import com.bj58.mis.apm.apm.scf.service.*;
import com.bj58.mis.apm.apm.scf.util.FileUtil;
import com.bj58.pay.core.utils.PageList;
import com.bj58.spat.scf.client.SCFInit;
import com.bj58.spat.scf.client.proxy.builder.ProxyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
import java.time.LocalDateTime;


/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhangyu84
 * @create 2018/7/6
 * @since 1.0.0
 */
@RestController
public class AuthController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private CubeBaseService cubeBaseService;
    private ProjectService projectService;
    private CubeStep4Service cubeStep4Service;
    private  AuthController(@Value("${scf.path}") String tempPath) {
        //调用SCF服务
        //1.准备本地scf.conf配置文件路径
        //String scfconf = System.getProperty("user.dir")+"\\src\\main\\resources\\scf.config";
        InputStream inputStream =AuthController.class.getClassLoader().getResourceAsStream("scf.config");
        FileUtil.createFile(inputStream,tempPath);
        //2.scf服务初始化
        SCFInit.init(tempPath);
        //3.准备SCF服务访问地址
        String cubeUrl = "tcp://rocserver/cubeBaseService";
        String projectUrl = "tcp://rocserver/projectService";
        String cubeUrl1 = "tcp://rocserver/cubeStep4Service";
        //4.调用SCF服务
        cubeBaseService = ProxyFactory.create(CubeBaseService.class, cubeUrl);
        projectService = ProxyFactory.create(ProjectService.class, projectUrl);
        cubeStep4Service = ProxyFactory.create(CubeStep4Service.class, cubeUrl1);
    }
    @Autowired
    private AuthService authService;
    /**
     * 从scf服务获取cube project基本信息 保存到库
     */
    @RequestMapping("auth")
    @Scheduled(cron = "0 0/5 * * * ?")
    public void authScheduled(){
        logger.info("===================AuthScheduleStart:"+LocalDateTime.now()+"========================");
        try {
            PageList<CubeBase> cubeBasePageList=cubeBaseService.getPageList(new CubeBase(),1,1000,null,true,null);
            PageList<Project> projectPageList = projectService.getPageList(new Project(),1,1000,null,true,null);
           authService.auth(cubeBasePageList.getList(),projectPageList.getList(),cubeStep4Service);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("===================AuthScheduleEnd:"+LocalDateTime.now()+"========================");
    }

}
